import Home from '@/components/Home'
/*import News from '@/components/News'
import Books from '@/components/Books'
import Video from '@/components/Video'
import Book from '@/components/Book'
import Login from '@/components/Login'*/
import {createRouter, createWebHistory} from 'vue-router'

const routes = [
  {
    path: '/', 
    redirect: {
      name: 'news'
    },
    name: 'home',
    component: Home
  },
  {
    path: '/news', 
    name: 'news',
    component: () => import('@/components/News'),//News,
    meta: {
      title: '新闻'
    }
  },
  {
    path: '/books', 
    name: 'books',
    component: () => import('@/components/Books'),//Books,
    meta: {
      title: '图书列表'
    }
    /*children: [
      {path: '/book/:id', name: 'book', component: Book}
    ]*/
  },
  {
    path: '/video', 
    name: 'video',
    component: () => import('@/components/Video'),//Video,
    meta: {
      title: '视频'
    }
  },
  {
    path: '/book/:id', 
    name: 'book',
    components: {
      bookDetail: () => import('@/components/Book'),//Book
    },
    meta: {
      title: '图书'
    }
  },
  {
    path: '/login', 
    name: 'login',
    component: () => import('@/components/Login'),//Login,
    meta: {
      title: '登录'
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})
router.beforeEach((to) => {
  if(to.path === '/login'){
    return true
  }
  else if(sessionStorage.getItem("isAuth") === "true"){
    return true
  }
  else{
    return {
      path: '/login',
      query: {redirect: to.fullPath}
    }
  }
})

router.afterEach(to => {
  document.title = to.meta.title
})
export default router

